﻿Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Web.UI.DataVisualization.Charting


Public Class Estadisticas
    Inherits System.Web.UI.Page
    Private strCalDesde, strCalHasta As String

    Protected Sub btnGenerar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerar.Click

        Dim param(0) As ADODB.Parameter
        Dim rs As ADODB.Recordset
        Dim dt As New DataTable
        Dim titleChart As String

        'Elijo reporte a mostrar del combo

        Select Case (cboCriterioBusq.SelectedValue)

            Case "Tasa bruta de natalidad"
                rs = Datos.TraerTodos("traerNatalidadBruta", Nothing, "")
                titleChart = "Tasa bruta de natalidad"

            Case "Tasa bruta de mortalidad general"
                rs = Datos.TraerTodos("traerMortalidadBruta", Nothing, "")
                titleChart = "Tasa bruta de mortalidad general"

            Case "Tasa de mortalidad infantil"
                rs = Datos.TraerTodos("traerMortalidadInfantil", Nothing, "")
                titleChart = "Tasa de mortalidad infantil"

            Case "Porcentaje de cobertura de BCG en menores de un año"
                rs = Datos.TraerTodos("traerBGCMenores", Nothing, "")
                titleChart = "Porcentaje de cobertura de BCG en menores de un año"
                'Case "Médicos cada 100 pacientes"
                '    rs = Datos.TraerTodos("traerMedicosPaciente", Nothing, "")
            Case Else
                rs = Datos.TraerTodos("traerMedicosPaciente", Nothing, "")
                titleChart = "Médicos cada 100 pacientes"

        End Select

        'Traigo los datos a mostrar
        dt = FG.cargaUnDataTable(rs)

        'Creo seteo ChartArea1
        Dim ChartArea1 As ChartArea = New ChartArea()
        ChartArea1.Name = "ChartArea1"

        'Seteo Titulo Encabezado Chart 
        Chart1.Titles.Add("TituloChart")
        Chart1.Titles(0).Text = titleChart
        Chart1.Titles(0).TextStyle = DataVisualization.Charting.TextStyle.Shadow
        Chart1.Titles(0).TextOrientation = DataVisualization.Charting.TextOrientation.Horizontal
        Chart1.Titles(0).Docking = DataVisualization.Charting.Docking.Top
        Chart1.Titles(0).Alignment = Drawing.ContentAlignment.MiddleCenter
        Chart1.ChartAreas.Add(ChartArea1)
        Chart1.Width = 600
        Chart1.Height = 600
        Chart1.ChartAreas(0).AxisX.Interval = 1

        With Chart1.ChartAreas(0).Area3DStyle
            .Enable3D = True
            .Inclination = 40
        End With

        'Le ponemos el titulo al Chart y al Panel del Popup
        lblpnlpopup.Text = titleChart

        'Cargo los datos con el DataTable al Chart1
        Chart1.DataSource = dt

        'Creo la Serie1
        Chart1.Series.Add(getSeries("Series1"))

        'Implemento Series
        Chart1.Series(0).Points.Clear()
        Chart1.Series(0).XValueMember = dt.Columns(0).ColumnName
        Chart1.Series(0).YValueMembers = dt.Columns(1).ColumnName

        Chart1.DataBind()

        'Se elige el grafico a mostrar segun lo que se selecciona en el Combo
        Dim nSeriesChartType As Integer
        Select Case (cboTipoGraf.SelectedValue)

            Case "Gráfico de torta"
                nSeriesChartType = DataVisualization.Charting.SeriesChartType.Pie
            Case "Gráfico de barras"
                nSeriesChartType = DataVisualization.Charting.SeriesChartType.Bar

                If dt.Columns.Count > 2 Then

                    Chart1.Series.Add(getSeries("Serie2"))
                    Chart1.Series.Add(getSeries("Serie3"))
                    Chart1.Series.Add(getSeries("Serie4"))
                    Chart1.Series.Add(getSeries("Serie5"))

                    For i = 1 To Chart1.Series.Count - 1
                        Chart1.Series(i).YValueMembers = dt.Columns(0).ColumnName
                        Chart1.Series(i).YValueMembers = dt.Columns(i + 1).ColumnName
                    Next i

                End If

            Case "Gráfico de área"
                nSeriesChartType = DataVisualization.Charting.SeriesChartType.Area
        End Select

        For i = 0 To Chart1.Series.Count - 1
            Chart1.Series(i).ChartType = nSeriesChartType
            Chart1.Series(i)("PieLabelStyle") = "outside"
        Next i
        Chart1.ChartAreas(0).AlignmentOrientation = AreaAlignmentOrientations.Horizontal

        Me.btnInforme.Visible = True
        'Chart1.Visible = True

        gvIndicadores.DataSource = FG.cargaUnDataTable(rs)
        gvIndicadores.DataBind()

    End Sub
    Public Function getSeries(ByVal nameSerie As String) As Series
        Dim Series1 As Series = New Series
        Series1.ChartArea = "ChartArea1"
        Series1.Legend = "Legend1"
        Series1.Name = nameSerie
        Series1.ChartType = SeriesChartType.Line
        Series1.BorderWidth = 3
        getSeries = Series1

        'Creo la Serie2
        'Series2.ChartArea = "ChartArea1"
        'Series2.Legend = "Legend1"
        'cSerie = "Serie2"
        'Series2.Name = cSerie
        'Series2.ChartType = SeriesChartType.Line
        'Series2.BorderWidth = 3
        'Chart1.Series.Add(Series2)

    End Function

    Public Function GetRandomSeries(ByVal name As String) As Series
        Dim returnSeries As New Series(name)
        Dim rnd As New Random()
        For index As Integer = 1 To 5
            returnSeries.Points.Add(rnd.Next(0, 100))
        Next
        Return returnSeries
    End Function



    Protected Sub btnInforme_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInforme.Click

        mdlpopup.Show()

    End Sub

    Private Sub Estadisticas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                FG.culturaPaginas()


                'Chart1.Visible = False
                'Cargo páginas anterior y actual!!!
                Var_Globales.Setea_paginas("Estadisticas.aspx")
            Else
                If Request.Form("__EVENTTARGET") = "FechaDesde" Then
                    txtCalDesde.Text = Request.Form("__EVENTARGUMENT")
                    strCalDesde = txtCalDesde.Text
                End If
                If Request.Form("__EVENTTARGET") = "FechaHasta" Then
                    txtCalHasta.Text = Request.Form("__EVENTARGUMENT")
                    strCalHasta = txtCalHasta.Text
                End If
            End If
        Catch ex As Exception
        End Try

    End Sub


End Class
